<?php

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

require __DIR__ . '/../../vendor/autoload.php';

$config = require(__DIR__ . '/../config.php');
$connection = new AMQPStreamConnection(
    $config['host'],
    $config['port'],
    $config['user'],
    $config['password'],
    $config['vhost']
);

const CONFIRM_EXCHANE = 'confirm.exchange';
$channel = $connection->channel();

$channel->set_ack_handler(
    function (AMQPMessage $message) {
        echo 'Message acked with content ' . $message->body . PHP_EOL;
    }
);

$channel->set_nack_handler(
    function (AMQPMessage $message) {
        echo 'Message nacked with content ' . $message->body . PHP_EOL;
    }
);

$channel->set_return_listener(
    function ($replyCode, $replyText, $exchange, $routingKey, AMQPMessage $message) {
        echo 'Message returned with content ' . $message->body . PHP_EOL;
    }
);

$channel->confirm_select();
$channel->exchange_declare(CONFIRM_EXCHANE, 'direct', false, false, true);

$confirmed = [];
$i = 1;
while(true) {
    if($i>10) {
        break;
    }
    echo "index". $i . PHP_EOL;
    $msg2 = new AMQPMessage('test111' . $i);

    $confirmed[$i] = $msg2;
    $channel->basic_publish($msg2, CONFIRM_EXCHANE, 'rk_confirm', true);
    $i++;
}

echo "ok" . PHP_EOL;

$channel->wait_for_pending_acks_returns();
$channel->close();
$connection->close();